var NavListMap = new Object();
var NavList =  function(opt){
	this.target = opt.target?opt.target:'nav';
	this.content = opt.content?opt.content:'pTypeList';
	this.topDiv = opt.topDiv?opt.topDiv:'pTypeBoxUp';
	this.bottomDiv = opt.bottomDiv?opt.bottomDiv:'pTypeBoxDown';
	this.disNum = opt.disNum?opt.disNum:8;				//导航显示个数
	this.focusClass = opt.focusClass?opt.focusClass:'_typeCurItem';
	this.normalClass = opt.normalClass?opt.normalClass:'_typeItem';
	this.clickClass = opt.clickClass?opt.clickClass:'_typeClickItem';
	
	this.itemHeight = opt.itemHeight?opt.itemHeight:52;	//分类高度
	this.defaultColor = opt.defaultColor?opt.defaultColor:'#fff';
	this.selectColor = opt.selectColor?opt.selectColor:'#ff0';
	
	this.onfocus = opt.onfocus?opt.onfocus:false;		//焦点在导航上
	this.onblur = opt.onblur?opt.onblur:false;		//焦点在item上
	this.onclick = opt.onclick?opt.onclick:false;		//点击事件
	this.beforeOnclick = opt.beforeOnclick?opt.beforeOnclick:false;		//点击事件
	
	if(opt.formatHtml){
		this.formatHtml = opt.formatHtml;
	}
	
	this.curTypeId = 0;
	this.focusTypeId = 0;
	this.curTopTypeId = 0;
	
	this.typeList = new Array();
	NavListMap[this.target] = this;
}

NavList.prototype = {
	getData:function(id){
		var data = false;
		if(this.typeList.length > 0){
			if(id){
				data = this.typeList[id - 1];
			}else{
				data = this.typeList[this.focusTypeId - 1];
			}
		}
		return data;
	},
	getCurData:function(){
		var data = false;
		if(this.typeList.length > 0){
			data = this.typeList[this.curTypeId - 1];
		}
		return data;
	},
	setFocus:function(id){
		var a = false;
		if(id){
			a = document.getElementById(this.target+'-a-'+id);
		}else{
			a = document.getElementById(this.target+'-a-'+this.focusTypeId);
		}
		if(a){	
			a.focus();
		}
	},
	setClick:function(id,ref){
		var a = false;
		if(id){
			a = document.getElementById(this.target+'-a-'+id);
		}else{
			a = document.getElementById(this.target+'-a-'+this.curTypeId);
		}
		
		if(a){
			if(ref){
				this.curTypeId = -1;
			}
			a.click();
		}
	},
	setDataList:function(list,onfocus){
		this.typeList = list;
		this.curTypeId = 0;
		this.focusTypeId = 1;
		this.curTopTypeId = 0;
		var content = document.getElementById(this.content);
		content.style.top = (0 - this.curTopTypeId * this.itemHeight)+'px';
		
		var typeBottom = document.getElementById(this.bottomDiv);
		var typeTop = document.getElementById(this.topDiv);
		
		typeBottom.style.display ='none';
		typeTop.style.display = 'none';
		
		this.refreshContent();
		if(onfocus){
			var a = document.getElementById(this.target+'-a-'+this.focusTypeId); 
			if(a){
				a.focus();
			}
		}
	},
	refreshContent:function(){
		var con = '';
		if(this.typeList){
			for(var i=0;i<this.typeList.length;i++){
				con +='<div class="'+this.normalClass+'">'+this.formatHtml(this.typeList[i],i+1)+'</div>';
			}
		}
		var box = document.getElementById(this.content);
		box.innerHTML = con;
		
		var aList = box.getElementsByTagName('a');
		var a;
		for(var i = 0;i<aList.length;i++){
			a = aList[i];
			a.onfocus = this.onfocusItem;
			a.onblur = this.onblurItem;
			a.onclick = this.onclickItem;
		}
	},
	formatHtml:function(type,id){
		var con = '<div><a href="#" id="'+this.target+'-a-'+id+'">'+type.name+'</a></div>';
		return con;
	},
	onfocusItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode.parentNode;
		var id = a.id;
		var idArray = id.split('-');
		var _this = NavListMap[idArray[0]];
		var tar = parseInt(idArray[2]);
		
		par.className = _this.focusClass;
		
		_this.changePos(tar);
		_this.focusTypeId = tar;
		if(_this.onfocus){
			var data = _this.typeList[tar - 1];
			_this.onfocus(data,tar);
		}
	},
	changePos:function(tar){
		if(this.disNum >= this.typeList.length) return;
		if(tar != this.focusTypeId ){
			if(tar - this.curTopTypeId == this.disNum + 1){
				this.curTopTypeId ++;
				var box = document.getElementById(this.content);
				box.style.top = (0 -this.curTopTypeId * this.itemHeight);
			}else if(tar == this.curTopTypeId){
				this.curTopTypeId --;
				var box = document.getElementById(this.content);
				box.style.top = (0 -this.curTopTypeId * this.itemHeight);
			}
			
			
			var typeBottom = document.getElementById(this.bottomDiv);
			var typeTop = document.getElementById(this.topDiv);
			if(tar - this.curTopTypeId == this.disNum && tar != this.typeList.length){
				typeBottom.style.display = 'block';
			}else{
				typeBottom.style.display = 'none';
			}
			if(tar-this.curTopTypeId == 1 && tar!=1){
				typeTop.style.display = 'block';
			}else{
				typeTop.style.display = 'none';
			}
			
		}
		
	},
	onblurItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode.parentNode;
		var id = a.id;
		var idArray = id.split('-');
		var _this = NavListMap[idArray[0]];
		var tar = parseInt(idArray[2]);
		
		if(tar == _this.curTypeId){
			par.className = _this.clickClass;
		}else{
			par.className = _this.normalClass;
		}
		if(_this.onblur){
			var data = _this.typeList[tar - 1];
			_this.onblur(data,tar);
		}
	},
	onclickItem:function(event){
		var a = event.currentTarget;
		var id = a.id;
		var idArray = id.split('-');
		var _this = NavListMap[idArray[0]];
		var tar = parseInt(idArray[2]);
		var data = _this.typeList[tar - 1];
		
		if(_this.beforeOnclick){		//如果点击事件之前触发
			var bo = _this.beforeOnclick(data);
			if(!bo){
				return false;
			}
		}
		
		
		var lastTypeA = document.getElementById(idArray[0]+'-a-'+_this.curTypeId);
		if(lastTypeA && tar!=_this.curTypeId){
			var par = lastTypeA.parentNode.parentNode;
			par.className = _this.normalClass;
		}
		_this.curTypeId = tar;
		if(_this.onclick){
			
			_this.onclick(data,tar);
		}
	},
}